import Vue from 'vue'
import VueRouter from 'vue-router'
import index from '@/views/index/index'
import vocabulary from '@/views/vocabulary/vocabulary'
import examine from '@/views/examine/examine'
import record from '@/views/record/record'
import discuss from '@/views/discuss/discuss'
import about from '@/views/about/about'
import center from '@/views/center/center'
import error404 from '@/views/error/404'
import login from '@/views/login/login'
import forgotpassword from '@/views/forgotpassword/forgotpassword.vue'
import register from '@/views/register/register.vue'
import review from '@/views/review/review.vue'

Vue.use(VueRouter)

//解决路由报错，路由重复点击报错
//获取原型上的push函数
const originalPush = VueRouter.prototype.push
// 获取原型对象replace函数
const originalReplace = VueRouter.prototype.replace

// 修改原型对象中的push函数
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

// 修改原型对象中的replace函数
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => err)
}


const routes = [
  {
    path:'/',
    //路由重定向，当用户访问根路径时，自动跳转到/login
    redirect:'/login'
  },
  {
    path: '/index',
    name: 'index',
    component: index
  },
  {
    path:'/login',
    name:'login',
    component:login,
   },
  {
    path: '/vocabulary',
    name: 'vocabulary',
    component: vocabulary
  },
  {
    path: '/examine',
    name: 'examine',
    component: examine
  },
  {
    path: '/record',
    name: 'record',
    component: record
  },
  {
    path: '/discuss',
    name: 'discuss',
    component: discuss
  },
  {
    path: '/about',
    name: 'about',
    component: about
  },
  {
    path: '/review',
    name: 'review',
    component: review
  },
  {
    path: '/center',
    name: 'center',
    component: center
  },
  {
    path: '/forgotpassword',
    name: 'forgotpassword',
    component: forgotpassword
  },
  {
    path: '/register',
    name: 'register',
    component: register
  },
  {
    path: '/404',
    name: '404',
    component: error404,
  },
  { 
    path: '/:pathMatch(.*)',
    redirect: '/404'
 },
]


const router = new VueRouter({
  mode: 'history',
  routes
})

export default router
